﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace nave
{
    class Tiro
    {
        Texture2D textura;
        public Vector2 posicao, posicaoDestino;
        float velocidade;
        public bool Ativo;
        public TimeSpan AnimationLengthSeconds;// = TimeSpan.FromSeconds(100f);
        TimeSpan AnimationPosition;
        public string textoTiro;

        public Tiro(Texture2D textura, Vector2 posicao, Vector2 posDestino, float tempo)
        {
            this.textura   = textura;
            this.posicao   = posicao;
            this.posicaoDestino = posDestino;
            this.Ativo = true;
            this.velocidade = 7f;
            this.textoTiro = "";
            this.AnimationLengthSeconds = TimeSpan.FromSeconds(tempo);
        }

        public void Update(GameTime gameTime)
        {
            this.posicao.Y -= velocidade;
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(textura, posicao ,Color.White);
        }

        public void AutoMoveTiro(GameTime gameTime, Vector2 posInicial, Vector2 posFinal, TimeSpan tempo)
        {
            if (tempo != null) { AnimationLengthSeconds = tempo; } else { AnimationLengthSeconds = TimeSpan.FromSeconds(50f); }

            if (AnimationPosition < AnimationLengthSeconds)
            {
                this.AnimationPosition += gameTime.ElapsedGameTime;
                if (this.AnimationPosition >= this.AnimationLengthSeconds)
                    this.AnimationPosition = this.AnimationLengthSeconds;
                var LerpAmount = (float)this.AnimationPosition.Ticks / this.AnimationLengthSeconds.Ticks;
                //this.textoTiro = LerpAmount.ToString();
                this.posicao = Vector2.Lerp(posInicial, posFinal, LerpAmount);
            }
        }

        //public static Vector2 Lerp(Vector2 from, Vector2 to, float t)
        //{
        //    t = MathHelper.Clamp(t,0,1);
        //    return new Vector2(from.X + ((to.X - from.X) * t), from.Y + ((to.Y - from.Y) * t));
        //}
    }
}